package com.atguigu.servicevod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exception.GuliException;
import com.atguigu.servicevod.service.VideoService;
import com.atguigu.servicevod.utils.AliyunVodSDKUtils;
import com.atguigu.servicevod.utils.ConstantVodUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author JY
 * @create 2021-01-24 12:12
 */
@Api(description="阿里云视频点播微服务")
@RestController
@RequestMapping("vod/video")
//@CrossOrigin
public class VideoController {
    @Autowired
    private VideoService videoService;

    //上传视频文件
    @PostMapping("upload")
    public R upload(MultipartFile file){
        //返回视频在阿里云唯一id
        String videoId = videoService.uploadVideo(file);
        return R.ok().data("videoId",videoId);
    }

    //根据视频id删除阿里云视频
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        try {
            //初始化阿里云对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantVodUtil.ACCESS_KEY_ID,
                    ConstantVodUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            //支持传入多个视频ID，多个用逗号分隔
            request.setVideoIds(id);
            //调用初始化对象实现删除
            client.getAcsResponse(request);
            return R.ok();
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败");
        }
    }

    //批量删除视频
    @DeleteMapping("delete-batch")
    public R deleteBatch(@RequestParam("videoIdList")List<String> videoIdList){
        videoService.deleteBatch(videoIdList);
        return R.ok();
    }

    //得到视频播放凭证
    @GetMapping("getPlayerAuth/{id}")
    public R getPlayerAuth(@PathVariable String id){
        try {
            //获取阿里云存储相关常量
            String accessKeyId = ConstantVodUtil.ACCESS_KEY_ID;
            String accessKeySecret = ConstantVodUtil.ACCESS_KEY_SECRET;

            //初始化对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret);

            //请求
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);

            //响应
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);

            //得到播放凭证
            String playAuth = response.getPlayAuth();

            return R.ok().message("获取凭证成功").data("playAuth", playAuth);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"获取凭证失败");
        }

    }
}
